package com.core.cloudcommon.trace;

/**
 * @author =====Lqj=====
 * @className TraceResultCode
 * @description 链路追踪响应编码
 * @create 2024/12/17 10:30
 **/
public enum TraceResultCode {
    
    // 链路追踪相关错误码 (800-899)
    TRACE_ID_NULL(800, "链路追踪ID不能为空"),
    TRACE_NOT_FOUND(801, "链路信息不存在"),
    TRACE_QUERY_FAILED(802, "链路日志查询失败"),
    TRACE_REPORT_FAILED(803, "链路报告生成失败"),
    TRACE_ID_INVALID(804, "链路ID格式不正确"),
    TRACE_PARAM_INVALID(805, "链路查询参数无效"),
    TRACE_LOG_READ_FAILED(806, "日志文件读取失败"),
    TRACE_PARSE_FAILED(807, "链路信息解析失败"),
    TRACE_TIMEOUT(808, "链路查询超时"),
    TRACE_ACCESS_DENIED(809, "链路信息访问被拒绝"),
    
    // 链路追踪业务错误码 (810-829)
    TRACE_USER_NOT_MATCH(810, "链路用户信息不匹配"),
    TRACE_TIME_RANGE_INVALID(811, "时间范围参数无效"),
    TRACE_COUNT_EXCEED_LIMIT(812, "查询数量超出限制"),
    TRACE_FILTER_INVALID(813, "过滤条件无效"),
    TRACE_EXPORT_FAILED(814, "链路数据导出失败"),
    
    // 链路追踪系统错误码 (830-849)
    TRACE_SYSTEM_ERROR(830, "链路追踪系统错误"),
    TRACE_CONFIG_ERROR(831, "链路追踪配置错误"),
    TRACE_STORAGE_ERROR(832, "链路数据存储错误"),
    TRACE_NETWORK_ERROR(833, "链路追踪网络错误"),
    TRACE_PERMISSION_ERROR(834, "链路追踪权限错误");

    private final Integer code;
    private final String message;

    TraceResultCode(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    /**
     * 获取错误码
     * @return 错误码
     */
    public Integer getCode() {
        return code;
    }

    /**
     * 获取错误消息
     * @return 错误消息
     */
    public String getMessage() {
        return message;
    }

    /**
     * 通过错误码获取枚举项
     * @param code 错误码
     * @return 枚举项
     */
    public static TraceResultCode getByCode(Integer code) {
        if (code == null) {
            throw new IllegalArgumentException("错误码不能为空");
        }
        for (TraceResultCode resultCode : TraceResultCode.values()) {
            if (resultCode.getCode().equals(code)) {
                return resultCode;
            }
        }
        throw new IllegalArgumentException("不存在的错误码: " + code);
    }

    /**
     * 检查是否为链路追踪相关错误码
     * @param code 错误码
     * @return true-是链路追踪错误码，false-不是
     */
    public static boolean isTraceErrorCode(Integer code) {
        return code != null && code >= 800 && code <= 899;
    }

    @Override
    public String toString() {
        return String.format("TraceResultCode{code=%d, message='%s'}", code, message);
    }
}
